package com.yuyou.tas.lesson.api;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.tas.lesson.service.ILessonClassStudentService;
import com.yuyou.tas.lesson.service.IStudentService;
import com.yuyou.tas.lesson.service.IUpGradeDataService;
import com.yuyou.tas.service.api.lesson.dto.MergeStuDTO;
import com.yuyou.tas.service.api.lesson.entity.LessonClassStudent;
import com.yuyou.tas.service.api.lesson.entity.Student;
import com.yuyou.tas.service.api.lesson.req.ReqFilterOrganStuOIds;
import com.yuyou.tas.service.api.lesson.service.StudentAPI;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;

@RestController
public class StudentAPIImpl implements StudentAPI{

	@Autowired
	private IStudentService studentService;
	
	@Autowired
	private ILessonClassStudentService lessonClassStudentService;
	
	@Autowired
	private IUpGradeDataService upGradeDataService;
	
	@Override
	public ResBoolSimpleInfo<String> sycnStudentByOId(@RequestBody List<Student> list)
			throws Exception {
		return studentService.sycnStudentByOId(list);
	
	}

	@Override
	public ResBoolSimpleInfo<String> delByOrgan7OIds(@RequestParam("oIds") List<Long> oIds,@RequestParam("authOrganizationId") Long authOrganizationId) throws Exception {
		return null;
	}

	@Override
	public ResBoolSimpleInfo<String> sycnStuClass(@RequestBody List<LessonClassStudent> list)
			throws Exception {
		return lessonClassStudentService.sycnStuClass(list);
	}

	@Override
	public Student selectByPrimaryKey(Long id) {
		return studentService.selectByPrimaryKey(id);
	}

	@Override
	public ResBoolSimpleInfo<String> sycnClassStudentData(String startTime) {		
    	//TODO 暂时
    	new Thread(new Runnable() {		
			@Override
			public void run() {						
				lessonClassStudentService.sycnClassStudentData(startTime);
			}
		}).start();
		
		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	public Student selectByOrgan7OId(@RequestParam("oId") Long oId,@RequestParam("authOrganizationId") Long authOrganizationId) {
		return studentService.selectByOrgan7OId(oId,authOrganizationId);
	}


	@Override
	public Student selectByOrganIdPhoneName(@RequestParam("authOrganizationId")Long authOrganizationId, @RequestParam("phoneNo")String phoneNo, @RequestParam("name")String name) {
		return studentService.selectByOrganIdPhoneName(authOrganizationId, phoneNo, name);
	}

	@Override
	public List<Student> filterOrganByOIds(@RequestBody ReqFilterOrganStuOIds reqFilterOrganStuOIds) {
		List<Student> studentList=new ArrayList<>();	
		reqFilterOrganStuOIds.getoIds().forEach(item ->{			
			Student student=studentService.selectByOrgan7OId(item, reqFilterOrganStuOIds.getAuthOrganizationId());
			if(student!=null){
				studentList.add(student);
			}
		});	
		return studentList;
	}

	@Override
	public ResBoolSimpleInfo<String> mergeStudent(MergeStuDTO mergeStuDTO)
			throws Exception {	
		List<MergeStuDTO> list=Collections.singletonList(mergeStuDTO);		
		return upGradeDataService.mergeStudent(list);
	}



}
